using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_GiftCouponEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        GiftCoupon giftCoupon;

        if (Int32.TryParse(Request["id"], out id))
            giftCoupon = GiftCoupon.Load(id);
        else
            giftCoupon = null;

        if (giftCoupon != null)
        {
            tbxPurchasedBySiteuserId.Text = giftCoupon.PurchasedBySiteuserId.ToString();
			tbxRecievedBySiteuserId.Text = giftCoupon.RecievedBySiteuserId.ToString();
			tbxGiftAmount.Text = giftCoupon.GiftAmount.ToString();
			if (giftCoupon.PurchaseDate != SqlDateTime.MinValue)
				tbxPurchaseDate.Text = giftCoupon.PurchaseDate.ToShortDateString();
			cbxIsUsed.Checked = giftCoupon.IsUsed;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        int purchasedBySiteuserId = Security.BindIntegerInput(tbxPurchasedBySiteuserId.Text, _securityLevel);
		int recievedBySiteuserId = Security.BindIntegerInput(tbxRecievedBySiteuserId.Text, _securityLevel);
		double giftAmount = Security.BindDoubleInput(tbxGiftAmount.Text, _securityLevel);
		DateTime purchaseDate = Security.BindDateTimeInput(tbxPurchaseDate.Text, _securityLevel);
		bool isUsed = cbxIsUsed.Checked;

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (GiftCoupon.Save(
				id, 
				purchasedBySiteuserId,
				recievedBySiteuserId,
				giftAmount,
				purchaseDate,
				isUsed
                ))
                    Response.Redirect("giftcouponlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            GiftCoupon giftCoupon = GiftCoupon.Create(
				purchasedBySiteuserId,
				recievedBySiteuserId,
				giftAmount,
				purchaseDate,
				isUsed
                );

            if (giftCoupon != null)
                Response.Redirect("giftcouponlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("giftcouponlist.aspx");
    }
}
